//
//  StyleViewController.m
//  ALFashion
//
//  Created by dengzhicheng on 15/6/13.
//  Copyright (c) 2015年 dengzhicheng. All rights reserved.
//

#import "StyleViewController.h"
#import <AFNetworking.h>
#import "StyleModel.h"
#import "StyleCollectionViewCell.h"
#import <UIImageView+WebCache.h>
#import "StyleDetailViewController.h"

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height

@interface StyleViewController ()<UICollectionViewDataSource, UICollectionViewDelegate, UIScrollViewDelegate>

@property (nonatomic, strong) NSMutableArray *tableArray;

@end

@implementation StyleViewController

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.translucent = NO;

    [self setNavigation];
    [self createSubView];
    [self startConnect];
    
}

// 设置导航栏
- (void)setNavigation
{
    // 自定义view, 添加到导航栏的titleView上
    UIView *titleView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 200, 30)];
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:titleView.frame];
    titleLabel.text = @"ALFashion";
    titleLabel.font = [UIFont fontWithName:@"huxiaobo-gdh" size:30];
    titleLabel.textAlignment = NSTextAlignmentCenter;
    [titleView addSubview:titleLabel];
    self.navigationItem.titleView = titleView;
    
    // 自定义button, 添加到导航栏的leftButtonItem上
    UIButton *menuButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 5, 20, 20)];
    [menuButton setImage:[UIImage imageNamed:@"menu.png"] forState:UIControlStateNormal];
    [menuButton setImage:[UIImage imageNamed:@"menuSelected.png"] forState:UIControlStateHighlighted];
    [menuButton addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:menuButton];
}


// menu 按钮点击事件
- (void)menuButtonClicked:(UIButton *)button
{
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"MenuView" object:@(YES)];
    
}

- (void)createSubView
{
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, WIDTH, 20)];
    [label setBackgroundColor:[UIColor colorWithRed:0.2 green:0.2 blue:0.2 alpha:1]];
    label.text = @"穿搭  STYLE";
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:12];
    label.textAlignment = NSTextAlignmentCenter;
    label.userInteractionEnabled = YES;
    [self.view addSubview:label];
}

- (void)startConnect
{
    NSString *str = @"http://van.hichao.com/stars?access_token=&gc=appStore&gender=2&gf=iPhone&gi=317f44c97afb1b5285a357b4b43bb6119fbe540d&gos=8.3&gv=1.0.2&offset=&region=&sign=27EEC628559AB395BC88558CD8D3785F&timestamp=1434507801.301623";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSData *data = operation.responseData;
        NSDictionary *bigDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableDictionary *smallDic = [bigDic objectForKeyedSubscript:@"content"];
        NSMutableArray *dataArr = [smallDic objectForKeyedSubscript:@"dataArr"];
        for (NSDictionary *dic in dataArr) {
            StyleModel *model = [[StyleModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
        }
        [self createCollectionView];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];

}


- (void)createCollectionView
{
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.itemSize = CGSizeMake(WIDTH / 2 - 20, 240);
    layout.sectionInset = UIEdgeInsetsMake(10, 10, 10, 10);
    UICollectionView *collection = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, HEIGHT - 84) collectionViewLayout:layout];
    [collection registerClass:[StyleCollectionViewCell class] forCellWithReuseIdentifier:@"cell"];
    collection.delegate = self;
    collection.dataSource = self;
    [self.view addSubview:collection];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    StyleCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    StyleModel *model = [self.tableArray objectAtIndex:indexPath.item];
    [cell.likeButton setTitle:model.likdCount forState:UIControlStateNormal];
    NSString *str = model.imageUrl;
    NSArray *array = [str componentsSeparatedByString:@"?"];
    NSString *url = [array firstObject];
    [cell.image sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"占位图.png"]];
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    StyleModel *model = [self.tableArray objectAtIndex:indexPath.item];
    StyleDetailViewController *style = [[StyleDetailViewController alloc] init];
    style.itemid = model.itemId;
    NSLog(@"----------------------------");
}







@end
